package leetcodecompetition.D375;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Siantar
 * @Date: 2023-12-10-11:01
 * @Description: 1.0
 */
public class T100137 {
    public long countSubarrays(int[] nums, int k) {
        List<Integer> list = new ArrayList<>();
        list.add(-1);
        int n = nums.length, maxNum = -1;
        for (int i = 0; i < n; i++) {
            if (nums[i] > maxNum) maxNum = nums[i];
        }
        for (int i = 0; i < n; i++) {
            if (nums[i] == maxNum) list.add(i);
        }
        int left = 1, size = list.size();
        long res = 0;
        while (left + k - 1 < size) {
            int right = left + k - 1;
            int leftIndex = list.get(left);
            int rightIndex = list.get(right);
            int leftPreIndex = list.get(left - 1);
            System.out.println(leftPreIndex+" "+leftIndex+" "+rightIndex+" ");
            res += (long) (leftIndex - leftPreIndex) * (n - rightIndex);
            left++;
        }
        return res;
    }
}
